package cn.syx.controller;

import cn.syx.domain.Account;
import cn.syx.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.IOException;
import java.util.List;

@Controller
@RequestMapping("account")
public class AccountController {
    @Autowired
    private AccountService accountService;

    @RequestMapping("save")
    @ResponseBody
    public String save(Account account) throws IOException {
        accountService.save(account);

        return "保存成功";
    }

    @RequestMapping("findAll")
    public String findAll(Account account, Model model) throws IOException {
        List<Account> accountList = accountService.findAll();
        model.addAttribute("accountList", accountList);

        return "accountList";
    }

    @RequestMapping("delete")
    public String delete(int id) throws IOException {
        accountService.deleteById(id);

        return "redirect:/account/findAll";
    }

    @RequestMapping("updateUI")
    public String updateUI(Account account, Model model) throws IOException {
        model.addAttribute("account", account);
        System.out.println(model);

        return "updateAccount";
    }

    @RequestMapping("update")
    public String update(Account account) throws IOException {
        accountService.updateAccount(account);

        return "redirect:/account/findAll";
    }

    @RequestMapping("transfer")
    public String transfer(String outMan, double money, String inMan) throws IOException {
        accountService.transfer(outMan, money, inMan);

        return "redirect:/account/findAll";
    }
}
